home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / std compression examples / example3.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  9.3 KB  |  311 lines

  1. /*
  2.     File:        Example3.c
  3.  
  4.     Contains:    Compression of PICT Files
  5.  
  6.                 The following sample code is again similar to the previous example except
  7.                 that it better deals with custom color tables, and demonstrates the use
  8.                 of extended procs and saving/restoring compression settings state.
  9.  
  10.     Written by:     
  11.  
  12.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 8/17/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 12/4/94        khs                changed the format of the file to the new look and feel
  25.  
  26. */
  27. // INCLUDE FILES
  28. #include <menus.h>
  29. #include <fonts.h>
  30. #include <osevents.h>
  31. #include <components.h>
  32. #include <quicktimecomponents.h>
  33.  
  34.  
  35. // FUNCTION PROTOTYPES
  36. pascal Boolean Example3FilterProc(DialogPtr theDialog,
  37.                                   EventRecord* theEvent,
  38.                                   short* itemHit,
  39.                                   long refcon);
  40. pascal short Example3HookProc(DialogPtr theDialog,
  41.                               short itemHit,
  42.                               void* params,
  43.                               long refcon);
  44. void Example3(void);
  45.  
  46.  
  47. // FUNCTIONS
  48. void Example3(void)
  49. {
  50.     ComponentResult result;
  51.     Point where;
  52.     ComponentInstance ci;
  53.     short sref;
  54.     SFTypeList typeList;
  55.     SFReply reply;
  56.     short firstFile = true;
  57.  
  58.     //    Open the Standard Compression Dialog component.
  59.  
  60.     ci = OpenDefaultComponent(StandardCompressionType, StandardCompressionSubType);
  61.     if (ci)
  62.     {
  63.  
  64.         //    Tell SFGetFilePreview to center on the best monitor.
  65.  
  66.         where.h = where.v = -2;
  67.  
  68.         //    Show only 'PICT' files in the file list.
  69.  
  70.         typeList[0] = 'PICT';
  71.  
  72.         //    Keep asking the user for files until they cancel.
  73.  
  74.         reply.good = true;
  75.         while (reply.good)
  76.         {
  77.  
  78.             //    Ask user to select PICT file to be compressed.
  79.  
  80.             SFGetFilePreview(where, "\p", nil, 1, typeList, nil, &reply);
  81.             if (reply.good)
  82.             {
  83.  
  84.                 //    If they selected a file, open that file.
  85.  
  86.                 result = FSOpen(reply.fName, reply.vRefNum, &sref);
  87.                 if (!result)
  88.                 {
  89.  
  90.                     //    If the file opened successfully, set the picture file to
  91.                     //    be the test image shown in the dialog.  Passing nil for srcRect
  92.                     //    means use the entire image.  Passing 0 for testFlags means
  93.                     //    to use the default system method of displaying the test image
  94.                     //    which is currently a combination of cropping and scaling.
  95.  
  96.                     SCSetTestImagePictFile(ci, sref, nil, 0);
  97.  
  98.                     //    If this is the first picture file being compressed do one-time
  99.                     //    setup of defaults and extended procs described below.
  100.  
  101.                     if (firstFile)
  102.                     {
  103.  
  104.                         //    Select defaults for the picture file.  Subsequent
  105.                         //    picture files will only have defaults chosen for
  106.                         //    them if the user presses the custom “Defaults” button
  107.                         //    in the compression dialog.
  108.  
  109.                         result = SCDefaultPictFileSettings(ci, sref, false);
  110.                         if (!result)
  111.                         {
  112.  
  113.                             //    Prepare and set the extended procs to be used by the
  114.                             //    compression dialog.  The filterProc is used to handle
  115.                             //    any events which the compression dialog doesn't know about.
  116.                             //    for
  117.  
  118.                             SCExtendedProcs xprocs;
  119.  
  120.                             //    The filterProc is used to handle any events which the
  121.                             //    compression dialog doesn't know about.  For example,
  122.                             //    the filterProc would handle update events for application
  123.                             //    windows uncovered by the movable modal compression dialog.
  124.                             //    The previous two examples don't supply a filterProc because
  125.                             //    they are not full applications with other windows and a menu
  126.                             //    bar.  However, all applications using the compression dialog
  127.                             //    should provide a filterProc to handle updates for their windows.
  128.                             //
  129.                             //    It is also possible to use the filterProc for update handling, etc.
  130.                             //    without using the custom button. Set the hookProc to nil and
  131.                             //    customName to "\p" and the custom button will not be displayed.
  132.  
  133.                             xprocs.filterProc = NewSCModalFilterProc(Example3FilterProc);
  134.  
  135.                             //    The hookProc handles clicks in the custom button and responds
  136.                             //    accordingly.  In our example, it will change the current settings
  137.                             //    to the defaults for the test image.
  138.  
  139.                             xprocs.hookProc = NewSCModalHookProc(Example3HookProc);
  140.  
  141.                             //    In this example, we pass the component instance of the
  142.                             //    compression dialog in as a refcon.  This value will then
  143.                             //    be available in our filter and hook procs to access the
  144.                             //    the compression dialog component.  This refcon could be
  145.                             //    any value that is useful to the filter and/or hook procs.
  146.  
  147.                             xprocs.refcon = (long)ci;
  148.  
  149.                             //    Copy the string for our custom button into the extended
  150.                             //    procs structure.
  151.  
  152.                             BlockMove("\pDefaults", xprocs.customName, 9);
  153.  
  154.                             //    Set the current extended procs to be our procs.
  155.                             //
  156.                             //    We could clear out the extended procs at any time
  157.                             //    by calling SCSetInfo(ci,scExtendedProcsType,nil);
  158.  
  159.                             result = SCSetInfo(ci, scExtendedProcsType, &xprocs);
  160.                         }
  161.  
  162.                         firstFile = false;
  163.                     }
  164.                     else
  165.                     {
  166.  
  167.                         //    If this is not the first pass, we probably want to clear out
  168.                         //    the color table chosen as the default in the first pass because
  169.                         //    it's most likely not appropriate for the current picture.  We could
  170.                         //    do this by calling SCSetInfo(ci,scColorTableType,nil) which would
  171.                         //    remove any custom color table completely.  The picture would then
  172.                         //    be compressed using the standard Macintosh color tables.
  173.                         //
  174.                         //    A better thing to do would be to keep the current defaults but
  175.                         //    set a new color table appropriate for the picture.  Unfortunately,
  176.                         //    there isn't a call to just get the default color table from the
  177.                         //    picture file.  That means saving the current settings, getting the
  178.                         //    default settings including a new color table, then restoring
  179.                         //    the previous settings while leaving the new color table.
  180.  
  181.                         Handle h;
  182.  
  183.                         //    Get a handle containing the current spatial compression settings.
  184.                         //    If we were dealing with sequences of images, this handle would also
  185.                         //    contain temporal and data rate settings.  The handle only includes
  186.                         //    information useful across reboots.  It does not include information
  187.                         //    such as the current test image, color table, window position, etc.
  188.                         //    The handle is created for us but we must dispose it.
  189.  
  190.                         result = SCGetInfo(ci, scSettingsStateType, &h);
  191.                         if (!result)
  192.                         {
  193.  
  194.                             //    Get default settings based on the picture file we are compressing.
  195.                             //    This includes the color table we will be interested in.
  196.  
  197.                             result = SCDefaultPictFileSettings(ci, sref, false);
  198.                             if (!result)
  199.  
  200.                             //    Restore the compression settings while leaving the color
  201.                             //    table set to the one from the current picture file.
  202.  
  203.                                 result = SCSetInfo(ci, scSettingsStateType, &h);
  204.  
  205.                             //    We have to throw away the handle created by SCGetInfo.
  206.  
  207.                             DisposeHandle(h);
  208.                         }
  209.                     }
  210.  
  211.                     //    If no errors have occured continue.
  212.  
  213.                     if (!result)
  214.                     {
  215.  
  216.                         //    Display the compress dialog to the user and request compression
  217.                         //    settings from them.  Note that the result code returned could
  218.                         //    include scUserCancelled.
  219.  
  220.                         result = SCRequestImageSettings(ci);
  221.                         if (!result)
  222.                         {
  223.  
  224.                             //    Compress the picture file with the settings chosen by the user.
  225.                             //    The settings include any custom color table found in the source
  226.                             //    picture if still appropriate for the depth chosen by the user.
  227.                             //
  228.                             //    Again note that we are able to pass the source file ref for both the
  229.                             //    source and destination picture files.  In this case, the picture
  230.                             //    file will be compressed in place.  It would probably be better to
  231.                             //    ask the user for a name to save the compressed file as, rather than
  232.                             //    compressing it in place.
  233.  
  234.                             result = SCCompressPictureFile(ci, sref, sref);
  235.                         }
  236.                     }
  237.  
  238.                     //    Close the source picture file.
  239.  
  240.                     FSClose(sref);
  241.                 }
  242.             }
  243.         }
  244.  
  245.         //    Close the Standard Compression Dialog component.
  246.  
  247.         CloseComponent(ci);
  248.     }
  249. }
  250.  
  251.  
  252. pascal Boolean Example3FilterProc(DialogPtr theDialog,
  253.                                   EventRecord* theEvent,
  254.                                   short* itemHit,
  255.                                   long refcon)
  256. {
  257. #pragma unused (itemHit, refcon)
  258.  
  259.     if (theEvent->what == updateEvt)
  260.     {
  261.         if ((WindowPtr)theEvent->message != theDialog)
  262.         {
  263.             // handle updates for application windows
  264.         }
  265.     }
  266.     return (false);
  267. }
  268.  
  269.  
  270. pascal short Example3HookProc(DialogPtr theDialog,
  271.                               short itemHit,
  272.                               void* params,
  273.                               long refcon)
  274. {
  275. #pragma unused (theDialog)
  276.  
  277.     //    Check to see if the item clicked in is our custom button.
  278.  
  279.     if (itemHit == scCustomItem)
  280.     {
  281.  
  282.         //    Let Standard Compression select some defaults for our picture file.
  283.  
  284.         SCDefaultPictFileSettings(params, *(short*)refcon, false);
  285.  
  286.         //    Note that we could set test image in here if we wanted.
  287.     }
  288.  
  289.     //    Always return the itemHit.
  290.  
  291.     return (itemHit);
  292. }
  293.  
  294.  
  295. // MAIN FUNCTION
  296. void main(void)
  297. {
  298.     InitGraf(&qd.thePort);
  299.     InitFonts();
  300.     FlushEvents(everyEvent, 0);
  301.     InitWindows();
  302.     InitMenus();
  303.     InitDialogs(nil);
  304.     InitCursor();
  305.     MaxApplZone();
  306.  
  307.     Example3();
  308. }
  309.  
  310.  
  311.